//
//  ViewController.m
//  段子
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "ContentModel.h"
#import "ImageTextTableViewCell.h"
#import "TextTableViewCell.h"
#import "GetRowHeightTools.h"



@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *contentsArray;
@property(nonatomic, retain)UITableView *tableView;

@end

@implementation ViewController

-(void)dealloc{
    [_tableView release];
    [_contentsArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self config];
    [self handleData];
    [self creatTableView];
}

-(void)config{
    self.navigationItem.title = @"网易";
    self.navigationItem.titleView.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.titleTextAttributes = @{
                                                                    NSForegroundColorAttributeName: [UIColor whiteColor],
                                                                    NSFontAttributeName: [UIFont fontWithName:@"American Typewriter" size:28]
                                                                    };
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"直播.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftLiveAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"serach.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightSearchAction:)];
    
    self.contentsArray = [NSMutableArray array];
    
    NSArray *fontNames = [UIFont familyNames];
    NSLog(@"%@", fontNames);
}
-(void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"duanzi" ofType:@".txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *webArray = [dic objectForKey:@"段子"];
    
    for (NSDictionary *dic in webArray) {
        ContentModel *content = [[ContentModel alloc]init];
        [content setValuesForKeysWithDictionary:dic];
        
        [self.contentsArray addObject:content];
    }
    
    for (ContentModel *model in self.contentsArray) {
        
        NSLog(@"图片：%@, 内容：%@, 个数：%ld", model.img, model.digest, self.contentsArray.count);
    }
    
}

-(void)creatTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100 - 64) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ImageTextTableViewCell class] forCellReuseIdentifier:@"imageTextCell"];
    [_tableView registerClass:[TextTableViewCell class] forCellReuseIdentifier:@"textCell"];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contentsArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ContentModel *model = [self.contentsArray objectAtIndex:indexPath.row];
    if (model.img == nil) {
        TextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        cell.model = model;
        return cell;
    }else{
        ImageTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageTextCell"];
        cell.model = model;
        return cell;
        
        
    }
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (!cell) {
//        
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
//    cell.textLabel.text = @"hah";
//    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ContentModel *model = [self.contentsArray objectAtIndex:indexPath.row];
    if (model.img == nil) {
        CGFloat height = [GetRowHeightTools heightWith:model.digest];
        return height + 80;
    }else{
        CGFloat height = [GetRowHeightTools heightWith:model.digest];
        CGFloat hei = [GetRowHeightTools imageHeightWith:model.imgsrc];
        return height + hei + 80;
        
        
    }

}

-(void)leftLiveAction:(UIBarButtonItem *)barButton{
    NSLog(@"开始直播啦!");
}
-(void)rightSearchAction:(UIBarButtonItem *)bar{
    NSLog(@"我要搜索");
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
